module.exports = class ResponseHelper {
  //请求出现错误时返回错误
  static sendErr(error, res) {
    let err;
    console.log("-------------error", error.errors);
    if (Array.isArray(error)) {
      console.log("-------------error1");
      err = error.join(";");
    } else if (error instanceof Error) {
      console.log("-------------error2");
      if(error.errors && error.errors.length > 0 ){
        const  ValidationErrorItem = error.errors[0];
        console.log("-------------ValidationErrorItem",ValidationErrorItem);
        if(ValidationErrorItem.type === 'unique violation' && ValidationErrorItem.message === 'user must be unique'){
            err = '账户已经存在!'
        }
      }else{
        err = error.message; 
      }
     
    } else {
      console.log("-------------error3");
      err = error;
    }
    res.send({
      err,
      msg: "请求出错",
      status: "fail",
      code: 450,
    });
  }
  //请求正常时返回结果
  static sendData(result, res) {
    res.status(200).send({
      status: "ok",
      msg: "请求成功",
      data: result,
      code: 200,
    });
  }
};
